<?=$this->form_eksternal->form_open("", '', 
                      array("id_detail" => $detail[0]->id_actifity_report))?>
<div id="main-content">
  <div class="container-fluid">
    <div class="row-fluid">
      <div class="span12">
        <div class="widget-block">
          <?php
          if($message == 'filed'){
          ?>
          <div class="alert alert-error fade in">
            <button data-dismiss="alert" class="close" type="button">×</button>
            <strong>Filed!</strong> Fail to save data.
          </div>
          <?php
          }
          if($message == 'sukses'){
          ?>
          <div class="alert alert-success fade in">
            <button data-dismiss="alert" class="close" type="button">×</button>
            <strong>Success!</strong> Now you have new user.
          </div>
          <?php
          }
          ?>
          <div class="widget-head">
            <h5><?=$title_table?></h5>
            <div class="widget-control pull-right">
              <a href="#" data-toggle="dropdown" class="btn dropdown-toggle"><i class="icon-cog"></i><b class="caret"></b></a>
              <ul class="dropdown-menu">
                <li><a href="<?php print site_url("outlet/tambahan/add-pica/".$detail[0]->id_actifity_report) ?>"><i class="icon-plus"></i> Add PICA</a></li>
                <li><a href="<?=  site_url("outlet/aktifitas/detail/".$detail[0]->id_actifity_report) ?>"><i class="icon-plus"></i> Report Detail</a></li>
                <li><a href="<?=  site_url("outlet/aktifitas/pica/".$detail[0]->id_actifity_report) ?>"><i class="icon-plus"></i> History</a></li>
<!--                <li><a href="#"><i class="icon-ok"></i> Active</a></li>
                <li><a href="#"><i class="icon-minus-sign"></i> No-Active</a></li>
                <li><a href="#"><i class="black-icons pdf_document"></i> Export to PDF</a></li>-->
            <!--  <li><a href="<? site_url("outlet/aktifitas/pica-exels/".$detail[0]->id_actifity_report) ?>"><i class="black-icons excel_document"></i> Export to XLS</a></li> -->
              </ul>
            </div>
          </div>
          <?php
          $cek_add_pica = $this->nbscache->get_olahan("permission", $this->session->userdata("id_privilege"), "add_pica", "add");
         $cek_edit_pica = $this->nbscache->get_olahan("permission", $this->session->userdata("id_privilege"), "add_pica", "edit");
         
          ?>
          <div class="widget-content">
            <div class="widget-box">
              <table id="table_pica" class="table">
                <thead>
                  <tr>
                    <th>Info</th>
                    <th width="150">Problem Identification</th>
                    <th width="150">Corrective Action</th>
                    <th>Date</th>
                    <th>Fix</th>
                  </tr>
                </thead>
                <?php
    
                foreach($parent as $key => $prn){
                  print <<<EOD
                  <tr>
                    <th colspan="8" style="background-color: aqua">{$prn}</th>
                  </tr>
EOD;
                  $link = site_url("outlet/aktifitas/add_history/{$key}");
                  if(is_array($data[$key])){
                    $no = 1;
                    foreach($data[$key] as $dt){
                        if(!$dt->problem)
                          $dt->problem = "Enter Suggest";
                        if(!$dt->corrective)
                          $dt->corrective = "Enter Suggest";
                        
                        $dtproblem = "";
                        $dtcorrective = "";
                      
                      $dtproblem = nl2br($dt->problem);
//                      $dtproblem = $this->form_eksternal->form_edit_textarea("problem_".$dt->id_history, 
//                              site_url("outlet/aktifitas/history-edit/{$dt->id_history}"), 
//                              $dt->problem, "type : 'textarea', rows : 8, cols : 2, submit    : 'OK', 
//                              callback : function(value, settings) {sign_user('author_problem_{$dt->id_history}')}");
                                
                      $dtcorrective = nl2br($dt->corrective);
//                      $dtcorrective = $this->form_eksternal->form_edit_textarea("corrective_".$dt->id_history, 
//                              site_url("outlet/aktifitas/history-edit/{$dt->id_history}"), $dt->corrective, 
//                              "type : 'textarea', rows : 8, cols : 2, submit    : 'OK', 
//                              callback : function(value, settings) {sign_user('author_crrective_{$dt->id_history}')}");
                      
                        $view_pic = "";
                        if($dt->id_pic_users > 0){
                          $view_pic_u = $this->global_models->get_field("d_pic_users", "id_users", array("id_pic_users" => $dt->id_pic_users));
                          $view_pic = $this->global_models->get_field("m_users", "name", array("id_users" => $view_pic_u));
                          $id_privi = $this->global_models->get_field("d_user_privilege", "id_privilege", array("id_users" => $view_pic_u));
                          $view_pic .= " | ". $this->global_models->get_field("m_privilege", "name", array("id_privilege" => $id_privi));
                        }
                      $create_user =  $this->global_models->get_field("m_users", "name", array("id_users" => $dt->create_by_users));
                          
                        $pic_users = $this->global_models->get_field("m_pic_pica", "name", array("id_pic_pica" => $dt->id_pic_pica));
//                        $pic_users = $this->form_eksternal->form_autocomplate("pic_view[{$dt->id_history}]", "pic_desk[{$dt->id_history}]", 
//                                "id_pic_view_{$dt->id_history}", "id_pic_desk_".$dt->id_history, site_url("outlet/aktifitas/pic-users"), 
//                                $view_pic, $dt->id_pic_users, "class='span8'",
//                                "$.ajax({
//                                    type: 'POST',
//                                    url: '".  site_url("outlet/aktifitas/post-pic-history")."',
//                                    data: { id: {$dt->id_history}, id_pic_users: ui.item.id }
//                                  }).done(function( msg ) {
//                                    check('check_{$dt->id_history}');
//                                  });"
//                                );
                        $tanggal_dou_date = $dt->due_date;
//                        $tanggal_dou_date = $this->form_eksternal->form_date("due_date_desk[{$dt->id_history}]", $dt->due_date, 
//                                'due_date_pic_desk_'.$dt->id_history, 'class="span10"', "
//                                  onSelect: function(dateText) {
//                                    $.ajax({
//                                      type: 'POST',
//                                      url: '".  site_url("outlet/aktifitas/post-due-date-history")."',
//                                      data: { id: {$dt->id_history}, due_date: dateText }
//                                    }).done(function( msg ) {
//                                      check('check_due_{$dt->id_history}');
//                                      sign_user('author_due_{$dt->id_history}');
//                                    });
//                                  },
//                                  ");
                                      if($dt->fix == 1){
                                        $status_id = TRUE;
                                        $disabled = "disabled";
                                      }else{
                                        $status_id = FALSE;
                                        $disabled ="";
                                      }
                          $cek_list = $this->form_eksternal->form_checkbox('cek[]', 0, $status_id, " id='cekprivilege_{$dt->id_history}' $disabled");
                          
                          
//                      lama laporan
                      $awal_time = strtotime($dt->tanggal_buat);
                      $selesai_time = strtotime($dt->tanggal_selesai);
                      $over_time = strtotime($dt->due_date);
                      if($dt->tanggal_selesai)
                        $sekarang = gregoriantojd(date("m", $selesai_time), date("d", $selesai_time), date("Y", $selesai_time));
                      else
                        $sekarang = gregoriantojd(date("m"), date("d"), date("Y"));
                      
                      $awal_laporan = gregoriantojd(date("m", $awal_time), date("d", $awal_time), date("Y", $awal_time));
                      $lama_laporan = $sekarang - $awal_laporan;
                      
                      if($dt->tanggal_selesai){
                        $selesai_laporan = gregoriantojd(date("m", $selesai_time), date("d", $selesai_time), date("Y", $selesai_time));
                        $lama_pengerjaan = $selesai_laporan - $awal_laporan;
                        $sekarang_over = $selesai_laporan;
                      }
                      else{
                        $lama_pengerjaan = "-";
                        $sekarang_over = $sekarang;
                      }
                      
                      $over_laporan = gregoriantojd(date("m", $over_time), date("d", $over_time), date("Y", $over_time));
                      $lama_kelewatan =  $sekarang_over - $over_laporan;
                                       
                      $data_hasil = "
                        <tr>
                          <td> Create date : {$dt->create_date} 
                            <br /> Create by : {$create_user}
                            <br /> Kategori : {$dt->kategori_view}
                            <br /> Sumber : {$dt->sumber_info_view}
                            <br /> PIC : {$pic_users}
                            <br /> Status : <span id='status_view_{$dt->id_history}'>{$dt->status_view}</span> <img id='check_{$dt->id_history}' style='display: none' src='{$url}img/checked.png' />
                          </td>
                          <td>{$dtproblem}</td>
                          <td>{$dtcorrective}</td>
                          <td> 
                            <span id='info_waktu_{$dt->id_history}'>Report Date : {$dt->tanggal_buat}
                            <br /> Due Date : {$dt->due_date}
                            <br /> Finish Date : {$dt->tanggal_selesai}
                            <br /> Lama Laporan : {$lama_laporan} hari
                            <br /> Lama Pengerjaan : {$lama_pengerjaan} hari
                            <br /> Over Due : {$lama_kelewatan} hari</span>
                          </td>
                        <td>{$cek_list}</td>
                        </tr>";
                          $data_hasil_2 = "
                        <tr>
                          <td>{$dt->create_date} <br /> {$create_user}</td>
                          <td>{$dt->problem}</td>
                          <td>{$dt->corrective}</td>
                          <td>{$view_pic}</td>
                          <td>{$dt->due_date}</td>
                            <td>{$cek_list}</td>
                        </tr>";
                      $no++;
                      $pic_users = "";
                      $tanggal_dou_date = "";
                      if($cek_edit_pica OR $this->session->userdata('id') == "1"){
                         $post_fix = site_url("outlet/post-fix");   
                                      print <<<EOD
   
                   <script>
                    $(document).ready(function(){
                     
                      $("#cekprivilege_{$dt->id_history}").click(function(){
                        if (($(this).attr('checked')) == 'checked') {
                            $.ajax({
                                    type: 'POST',
                                    url: '{$post_fix}',
                                    data: { id: {$dt->id_history}, fix: "1" }
                                  }).done(function( msg ) {
                                    check('check_{$dt->id_history}');
                                    $('#status_view_{$dt->id_history}').text('Finish');
                                    $('#info_waktu_{$dt->id_history}').html(msg);
                                  });
                                  eval($(this).prop("disabled", true));
                         }
                      });
                    });
                    </script>
                    
EOD;
                         print $data_hasil;
                      }else{
                         $post_fix = "";   
                        print $data_hasil_2;
                      }
          
                    }
                  }
                  
                  print <<<EOD
                  <tbody id="body_{$key}">
                  </tbody>
                  <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>  
EOD;
  print <<<EOD
                    
                    </td>
                  </tr>
EOD;
                }
                ?>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</form>
<script>
  function add_row_tambahan_sementara(table_pica, site_url, body_pica, param){
    add_row_tambahan(table_pica, site_url, body_pica, param);
  }

  function sign_user(id){
    $("#"+id).text('<?=$this->session->userdata('name')?>');
  }
  function check(id){
    $("#"+id).show();
  }
//  function check(id){
//    $("#"+id).show();
//  }
</script>